package com.scau.phradmin.business.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scau.phradmin.common.util.Md5Encipher;
import com.scau.phradmin.common.util.PageDTO;
import com.scau.phradmin.common.util.StrTool;
import com.scau.phradmin.domain.entity.User;
import com.scau.phradmin.domain.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private Md5Encipher md5Encipher;
    /**
     * 查询所有用户信息
     * @param pages
     * @return
     */
    public PageDTO<User> selectAll(int pages){
        Page page=null;
        if(pages>0) {
            page=new Page(pages,10);
        }
        else{
            page=new Page(1,10);
        }
        List<User> list=userMapper.queryAll(page);
        return PageDTO.valueOf(list,page);
    }

    /**
     * 通过tel查询唯一用户信息
     * @param tel
     * @return
     */
    public Object selectByTel(String tel){
        StrTool.sql_inj(tel);
        return userMapper.queryOne(tel);
    }

    /**
     * 通过主键修改唯一用户信息，成功返回true，否则返回false
     * @param user
     * @return
     */

    @Transactional
    public boolean edit(User user){
        return userMapper.updateById(user)>0;
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Transactional
    public boolean add(User user){
        user.setPassword(md5Encipher.encrypt(user.getPassword()));
        return userMapper.insert(user) > 0;
    }

    /**
     * 通过id删除唯一用户
     * @param id
     * @return
     */
    @Transactional
    public boolean delete(String id){
        StrTool.sql_inj(id);
        return userMapper.deleteById(id) > 0;
    }
}
